<template>
  <base-breadcrumb :title="$route.meta.title">
    <!-- 廉洁文化教育 -->
    <a-form-model
      ref="ruleForm"
      :model="form"
      :label-col="{ span: 6 }"
      :wrapper-col="{ span: 18 }"
      :rules="editModel != 0 ? rules : {}"
    >
      <a-spin :spinning="loading">
        <!-- 表格 -->
        <a-card :bordered="false">
          <title-name title="基本信息" />
          <custom-form
            :form-list="formList"
            :form="form"
            :edit-model="editModel == 0 ? 'V' : ''"
            :org-code.sync="form.level"
            :org-code-name.sync="form.levelName"
          >
            <!--参加人员-->
            <!--<template #personIds>-->
              <!--<a-tag-->
                <!--v-for="person in form.personIds"-->
                <!--:key="person.id"-->
                <!--:closable="editModel != 0"-->
                <!--color="#108ee9"-->
                <!--@close="form.personIds = form.personIds.filter((item) => person.id !== item.id)"-->
              <!--&gt;-->
                <!--{{ person.name }}-->
              <!--</a-tag>-->
              <!--<a-button-->
                <!--v-if="editModel != 0"-->
                <!--icon="plus"-->
                <!--:class="{ ml12: form.personIds ? form.personIds.length > 0 :false }"-->
                <!--@click="visible = true"-->
              <!--&gt;-->
                <!--添加人员-->
              <!--</a-button>-->
            <!--</template>-->
            <!-- 附件上传 -->
            <template #fileList>
              <template v-if="editModel != 0">
                <l-table
                  :file-auto-loading="false"
                  business-code="UN04"
                  :upload-arrys.sync="form.fileList"
                />
                <div class="file_prompt">
                  请上传原件，支持格式：.rar .zip .doc .docx .pdf .jpg .png .mp4 .mov
                </div>
              </template>
              <file-link
                v-else
                :file-list="form.fileList"
              />
            </template>
          </custom-form>
        </a-card>
      </a-spin>
      <!-- 页脚操作栏 -->
      <div class="btn_box page-btn-right-top">
        <a-button
          v-if="editModel != 0"
          :loading="loadingSub"
          type="primary"
          class="button_right"
          @click="handleSubmit()"
        >
          提交
        </a-button>
        <a-button
          type="primary"
          @click="handleActiveClose"
        >
          关闭
        </a-button>
      </div>
    </a-form-model>
    <!-- <SelectPersonModal-->
      <!--:persons="form.personIds"-->
      <!--:visible.sync="visible"-->
    <!--/>-->
  </base-breadcrumb>
</template>

<script>
  import { create, update, detail } from '@/api/disciplineInspection/culturalEducation/cltEdu.js' // api
  import BaseEditPage from '@/views/baseEditPage' // 编辑详情页基础配置
  import SelectSupevise from '@/components/Select/SelectSupevise'
  import { getEmployeesList } from '@/api/commons.js'
  import { getLabels } from '@/api/office/supervise.js'
  import { getEmployees } from '@/api/businessAffairs/revenueAndCost'
  import moment from 'moment';
  // import SelectPersonModal from '@/components/SelectPersonModal'

  export default {
    name:'a'+ Date.now(),
    extends: new BaseEditPage(),
    components: {
      // SelectPersonModal,
    },
    data() {
      return {
        loading: false, // 加载控件
        loadingSub: false, // 加载按钮
        // visible: false, //人员选择控件
        editModel: this.$route.query.editModel, // 操作状态:0 查看;1 编辑;2 新增
        form: {
          unitName:JSON.parse(sessionStorage.getItem('ycloud-user_info')).currentPost.name,
          level: JSON.parse(sessionStorage.getItem('ycloud-user_info')).currentPost.code,
          levelName: this.editModel == 0 ? '' : this.handleLevel(),
          // personIds: [],
        }, // 表单
        rules: {
          title: [{ required: true, message: '请输入会议名称', trigger: 'change' }],
          level: [{ required: true, message: '请选择层级', trigger: 'change' }],
          unitName: [{ required: true, message: '请选择单位', trigger: 'change' }],
          charge: [{ required: true, message: '请选择负责人', trigger: 'change' }],
          meetTime: [{ required: true, message: '请选择会议日期', trigger: 'change' }],
          address: [{ required: true, message: '请输入会议地址', trigger: 'change' }],
          content: [{ required: true, message: '请输入学习内容概述', trigger: 'change' }],
          // personIds: [{ required: true, message: '请输入参会人员', trigger: 'change' }],
          fileList: [{ required: true, message: '请上传文件', trigger: 'blur' }],
        }, // 校验规则
        init: false, // 初始化控件
        formCode: '', // 形式选中项code
        code: '', // 附件上传的businessCode
      }
    },
    watch: {
    },
    computed: {
      formList() {
        return [
          {
            keyName: 'title',
            viewName: 'title',
            type: 'a-input',
            label: '会议名称',
            ellipsis: true,
          },
          {
            keyName: 'unitName',
            viewName: 'unitName',
            type: 'a-input',
            label: '单位',
            placeholder:'请选择单位',
            data: [
              {
                value:JSON.parse(sessionStorage.getItem('ycloud-user_info')).currentPost.id,
                label:JSON.parse(sessionStorage.getItem('ycloud-user_info')).currentPost.name,
              }
            ],
            ellipsis: true,
          },
          // {
          //   keyName: 'unit',
          //   viewName: 'unitName',
          //   label: '单位',
          //   type: SelectCompanyAndProject,
          //   placeholder:'请选择单位',
          //   attrs:{
          //     labelInValue: true,
          //   },
          //   ellipsis: true,
          // },
          {
            type: 'BaseInput',
            keyName: 'levelName',
            disabled: true,
            placeholder: '请先选择层级',
            label: '层级',
          },
          // {
          //   type: Selector,
          //   keyName: 'level',
          //   viewName: 'levelName',
          //   label: '层级',
          //   // disabled:true,
          //   attrs: {
          //     selectType: '1',
          //     urlType: 'union',
          //     labelInValue: true,
          //   },
          // },
          {
            keyName: 'charge',
            viewName: 'chargeName',
            label: '负责人',
            type: SelectSupevise,
            placeholder:'请选择负责人',
            attrs:{
              orgId:JSON.parse(sessionStorage.getItem('ycloud-user_info')).currentPost.id,
              culEdu: true,
              labelInValue:true,
            },
            ellipsis: true,
          },
          {
            keyName: 'meetTime',
            type: 'a-date-picker',
            placeholder: '请选择会议日期',
            label: '会议日期',
            attrs: {
              valueFormat: 'YYYY-MM-DD',
              // disabledDate: (current) => {
              //   return current && current < moment().endOf('day')
              // }
            },
            ellipsis: true,
          },
          {
            keyName: 'address',
            viewName: 'address',
            type: 'a-input',
            label: '会议地址',
            ellipsis: true,
          },
          {
            keyName: 'content',
            type: 'BaseTextarea',
            label: '学习内容概述',
            autoSize:{ minRows: 10, maxRows: 10 },
            attrs: {
              maxLength : 1000,
            },
            ellipsis: true,
            span:24
          },
          // {
          //   slot: true,
          //   keyName: 'personIds',
          //   label: '参会人员',
          //   span: 24,
          // },
          {
            slot: true,
            keyName: 'fileList',
            label: '附件',
            span: 24,
          },
        ]
      },
    },
    methods: {
      /**
       * 默认层级
       */
      handleLevel(){
        let levelCode = JSON.parse(sessionStorage.getItem('ycloud-user_info')).currentPost.code;
        if(levelCode == 'PD'){
          return '项目部'
        }else if (levelCode == 'BC') {
          return '分公司'
        }else if (levelCode == 'CO') {
          return '公司'
        }
      },
      /**
       * 校验表单
       */
      handleSubmit() {
        // 提交表单
        this.$refs.ruleForm.validate((e) => {
          if (e) {
            this.handleSubmitApi()
          }
        })
      },
      /**
       * 提交表单
       */
      handleSubmitApi(){
        let that = this
        let api = [update, create][that.editModel - 1] // 根据操作切换url地址
        that.loading = true
        that.loadingSub = true
        let formData = that.$clone(that.form)
        let form={
          ...formData,
          fileId: formData.fileList.map(item =>{return item.commonId}).toString(), //上传文件id的数组转字符串
          // people: formData.personIds.map(item =>{return item.id}).toString(), //人员ID数组转字符串
          levelId: formData.level, //层级
          unitId: JSON.parse(sessionStorage.getItem('ycloud-user_info')).currentPost.id,
          chargeId: formData.charge.key,
          chargeName: formData.charge.label,
          orgId:JSON.parse(sessionStorage.getItem('ycloud-user_info')).currentPost.id
        }
        api(form)
          .then(() => {
            that.loading = false
            that.$message.success('提交成功！')
            that.$multiTab.close()
          })
          .catch(() => {
            that.loading = false
          })
      },

    },
    mounted(){
      this.$route.meta.title = ['廉洁文化教育查看', '廉洁文化教育编辑', '廉洁文化教育新增'][this.editModel]
      /**
       * 不为添加时请求详情
       * */
      if (this.editModel != 2) {
        this.loading = true
        detail(this.id)
          .then((res) => {
            this.loading = false;
            this.form = {
              ...res.data,
              // personIds: res.data.peoples,
              fileList : res.data.fileInfoVOS,
              charge: {
                key: res.data.chargeId,
                label:  res.data.chargeName,
              },
              unit: {
                value: res.data.unitId,
                label:  res.data.unitName,
              },
            };
            this.$nextTick(() => {
              this.init = true
            })
          })
          .catch(() => {
            this.loading = false
          })
      } else {
        this.$nextTick(() => {
          this.init = true
        })
      };
    }
  }
</script>

<style lang="less" scoped>
    /deep/ .ant-form-item {
        .ant-form-item-label {
            height: 28px;
            width: 200px;
            line-height: 22px;
            padding: 6px 4px 0 0;
            text-align: right;
            white-space: pre-wrap;
            overflow: visible;
        }
        .ant-form-item-control-wrapper {
            width: calc(100% - 200px);
        }
    }
    .file_prompt {
        color: #999;
    }
</style>